<?php

namespace application\Module;

use library\Core\Db\CDbCriteria;
use application\Model\AdminModel;
use library\Core\Config;

class LoginForm {

    /**
     * 登录系统
     * @param array $loginParam
     */
    public function login($loginParam = []){
        $status = true;
        $errors = [];

        $username = isset($loginParam['username']) ? $loginParam['username'] : '';
        $password = isset($loginParam['password']) ? $loginParam['password'] : '';
        $captcha = isset($loginParam['captcha']) ? $loginParam['captcha'] : '';

        if (empty($username)) {
            $status = false;
            $errors['username'] = '用户帐号不能为空';
        }
        if (empty($password)) {
            $status = false;
            $errors['password'] = '用户密码不能为空';
        }
        if (empty($captcha)) {
            $status = false;
            $errors['captcha'] = '验证码不能为空';
        }else{
            $captcha_session = $_SESSION['captcha'];
            if (strtolower($captcha) !== $captcha_session && !empty($captcha_session)) {//验证验证码是否相等
                $status = false;
                $errors['captcha'] = '验证码错误，请您重新输入';
            }
        }
        
        if ($status == true) {
            $passport = $this->getPassport($username);
            if(!$passport){
                $status = false;
                $errors['username'] = '帐号不存在，请您重新再试';
            }else{
                $validatePassword = $this->validatePassword($password, $passport['password']);
                if(!$validatePassword){
                    $status = false;
                    $errors['password'] = '密码不正确，请您重新再试';
                }else{
                    $user = $this->getUser($passport['id']);
                    if(!$user){
                        $status = false;
                        $errors['admin'] = '用户不存在';
                    }else{
                        $authorities = explode('|', trim($user['authority']));
                        $authorities = array_filter($authorities); //去除空值
                        $_SESSION['admin'] = [
                            'username' => $passport['username'],
                            'passport_id' => $passport['id'],
                            'id' => $user['id'],
                            'name' => $user['name'],
                            'role_id' => $user['role_id'],
                            'authority' => $authorities,
                        ];
                    }
                }
            }
            
        }
        return [
            'status' => $status,
            'errors' => $errors,
        ];
    }
    
    private function getPassport($username){
        $criteria = new CDbCriteria();
        $criteria->select([
            'id',
            'username',
            'password',
        ]);
        $criteria->addEqCondition('username', $username);
        $criteria->addEqCondition('status', 1);
        $passportModel = new AdminModel();
        return $passportModel->getRow($criteria);
    }
    
    private function validatePassword($password,$validate){
        return md5(md5(Config::getParamsVal('USERKEY').$password)) === $validate;
    }
    
    private function getUser($passport_id){
        $criteria = new CDbCriteria();
        $criteria->select([
            'id',
            'role_id',
            'name',
            'authority',
        ]);
        $criteria->addEqCondition('id', $passport_id);
        $userModel = new AdminModel();
        return $userModel->getRow($criteria);
    }
}
